/**
 * Visual Blocks Language
 *
 * Copyright 2012 Google Inc.
 * http://blockly.googlecode.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Generating Python for logic blocks.
 * @author gasolin@gmail.com  (Fred Lin)
 */
'use strict';

goog.provide('Blockly.Python.logic');

goog.require('Blockly.Python');
Blockly.Python.LOOP_PASS = '  pass\n';

Blockly.Python.control_repeat = function() {
  var value_name = Blockly.Python.valueToCode(this, 'TIMES', Blockly.Python.ORDER_NONE);
  var statements_name = Blockly.Python.statementToCode(this, 'SUBSTACK');
   var branch = Blockly.Python.addLoopTrap(statements_name, this.id) ||
      Blockly.Python.LOOP_PASS;
  // TODO: Assemble JavaScript into code variable.
  var loopVar = Blockly.Python.variableDB_.getDistinctName(
      'count', Blockly.Variables.NAME_TYPE);
  var code = 'for ' + loopVar + ' in range(' + value_name + '):\n' + branch;
  return code;
};

Blockly.Python.control_if = function() {
  // If condition.
  var value_name = Blockly.Python.valueToCode(this, 'CONDITION', Blockly.Python.ORDER_NONE) || 'False';
  var statements_ccc = Blockly.Python.statementToCode(this, 'SUBSTACK');
  var branch = Blockly.Python.addLoopTrap(statements_ccc, this.id) ||
      Blockly.Python.LOOP_PASS;
  // TODO: Assemble JavaScript into code variable.
  var code = 'if '+value_name+':\n'+branch+'\n';
  return code;
};



Blockly.Python.control_if_else = function() {
  // If condition.
  var value_name = Blockly.Python.valueToCode(this, 'CONDITION', Blockly.Python.ORDER_NONE) || 'False' ;
  var statements_if = Blockly.Python.statementToCode(this, 'SUBSTACK');
  var statements_else = Blockly.Python.statementToCode(this, 'SUBSTACK2');
  var branch = Blockly.Python.addLoopTrap(statements_if, this.id) ||
      Blockly.Python.LOOP_PASS;
  var branch_else = Blockly.Python.addLoopTrap(statements_else, this.id) ||
      Blockly.Python.LOOP_PASS;
  // TODO: Assemble JavaScript into code variable.
  var code = 'if '+value_name+':\n'+branch+'\nelse:\n'+branch_else+'\n';
  return code;
};



Blockly.Python.control_repeat_until = function() {
  var value_name = Blockly.Python.valueToCode(this, 'CONDITION', Blockly.Python.ORDER_NONE) || 'False';
  var statements_name = Blockly.Python.statementToCode(this, 'SUBSTACK');
  // TODO: Assemble JavaScript into code variable.
  var branch = Blockly.Python.addLoopTrap(statements_name, this.id) ||
      Blockly.Python.LOOP_PASS;

  var code = 'while '+value_name+':\n'+branch+'\n';

  return code;
};


Blockly.Python.control_jump_out_loop = function() {
    //跳出循环
  var code = 'break\n';

  code = Blockly.Python.addLoopTrap(code, this.id) ||
      Blockly.Python.LOOP_PASS;
  return code;
};
